import pkg from "../package.json";

const SD_SYSTEM = {
  // MAIN: "*://pmos.sd.sgcc.com.cn/http%3A%2F%2Fpmos.sd.sgcc.com.cn%2Ftrade%2Fmain%2Findex.do?*",
  MAIN: "*://pmos.sd.sgcc.com.cn/*",
  /** 现货市场 */
  SPOT_MARKET: "https://*/trade/main/index.do?*",
  /** 现货市场 中长期 */
  SPOT_MARKET_ZCQ: "https://*/zcq/main/index.do?*",
  /** 信息披露 */
  INFORMATION_DISCLOSURE: '*://pmos.sd.sgcc.com.cn/pxf-settlement-outnetpub/*'
}

const manifest:Partial<chrome.runtime.ManifestV3> = {
  permissions:['scripting','cookies', 'webRequest', 'tabs', 'storage', "offscreen"],
  action: {
    default_icon: {
      16: "icons/16.png",
      19: "icons/19.png",
      32: "icons/32.png",
      38: "icons/38.png",
    },
    default_popup: "src/entries/popup/index.html",
  },
  background: {
    service_worker: "src/entries/background/main.ts",
  },
  content_scripts: [
    {
      js: ["src/entries/contentScript/primary/main.ts"],
      matches: [
        "*://gdsxspot.junengcloud.com/*",
        SD_SYSTEM.MAIN,
        SD_SYSTEM.SPOT_MARKET
      ]
    },
    {
      js: ["src/entries/contentScript/informationDisclosure/main.ts"],
      matches: [
        "*://gdsxspot.junengcloud.com/*",
        "*://gitlab.huidiancloud.com/new-energy/frontend/sd-trade-data-extension/branches/*",
        SD_SYSTEM.INFORMATION_DISCLOSURE
      ]
    }
  ],
  host_permissions: [
    "*://juejin.cn/*", 
    "*://gdsxspot.junengcloud.com/*",
    "*://ies.huidiancloud.com/*",
    "*://pmos.sd.sgcc.com.cn/*",
    "*://120.78.68.153/",
    "<all_urls>"
  ],
  icons: {
    16: "icons/16.png",
    19: "icons/19.png",
    32: "icons/32.png",
    38: "icons/38.png",
    48: "icons/48.png",
    64: "icons/64.png",
    96: "icons/96.png",
    128: "icons/128.png",
    256: "icons/256.png",
    512: "icons/512.png",
  },
  options_ui: {
    page: "src/entries/options/index.html",
    open_in_tab: true,
  },
};

/** 根据打包环境 替换路径 */
const replaceIconsPath = (mode:string, icons:chrome.runtime.ManifestIcons):chrome.runtime.ManifestIcons => {
  const path = /^dev/.test(mode) ? 'iconsDev' : 'iconsProd'
  for(let key in icons) {
    icons[key] = icons[key].replace('icons/', `${path}/`)
  }
  return icons
}

/** 替换icons的路径 */
const replaceIcons = (mode:string):Partial<chrome.runtime.ManifestV3> => {
  const newManifest = JSON.parse(JSON.stringify(manifest))

  newManifest.action.default_icon = replaceIconsPath(mode, newManifest.action.default_icon as chrome.runtime.ManifestIcons)
  newManifest.icons = replaceIconsPath(mode, manifest.icons as chrome.runtime.ManifestIcons)

  return newManifest
}

/** 打包变量对应的账号 */
export const modeAccounts:Record<string, string> = {
  devFuXi: 'zzcn2023',
  prodFuXi: 'zzcn2023',
  prodFuXiH: 'zzcn2023',

  devGuiChang: 'a1257176370',
  prodGuiChang: 'a1257176370',
  prodGuiChangH: 'a1257176370',

  devFuXiFengDian: 'sg2020',
  prodFuXiFengDian: 'sg2020',
  prodFuXiFengDianH: 'sg2020',

  devMALTFuXiFengDian: 'sg2020',
  prodMALTFuXiFengDian: 'sg2020',
  prodMALTFuXiFengDianH: 'sg2020',

  devMALTZiBo: 'yys2020',
  prodMALTZiBo: 'yys2020',
  prodMALTZiBoH: 'yys2020',

  devMALTZhaoYuan: 'zx2020',
  prodMALTZhaoYuan: 'zx2020',
  prodMALTZhaoYuanH: 'zx2020',

  devFuShan: 'qysf4536',
  prodFuShan: 'qysf4536',
  prodFuShanH: 'qysf4536',

  devMALTFuShan: 'qysf4536',
  prodMALTFuShan: 'qysf4536',
  prodMALTFuShanH: 'qysf4536',

  devZhongNeng: 'znsd2024',
  prodZhongNeng: 'znsd2024',
  prodZhongNengH: 'znsd2024',

  devMALTZhongNeng: 'znsd2024',
  prodMALTZhongNeng: 'znsd2024',
  prodMALTZhongNengH: 'znsd2024',

  devLaiZhou: 'lzcn2024',
  prodLaiZhou: 'lzcn2024',
  prodLaiZhouH: 'lzcn2024',

  devHanTing: 'wfhtqhkxnyyxgs',
  prodHanTing: 'wfhtqhkxnyyxgs',
  prodHanTingH: 'wfhtqhkxnyyxgs',

  devJinXiang: 'hlys2016',
  prodJinXiang: 'hlys2016',
  prodJinXiangH: 'hlys2016',

  devTanCheng: 'tcwsxny',
  prodTanCheng: 'tcwsxny',
  prodTanChengH: 'tcwsxny',
  
  devYiYuan: 'xlzh20230630',
  prodYiYuan: 'xlzh20230630',
  prodYiYuanH: 'xlzh20230630',
}

export function getManifest(mode:string): chrome.runtime.ManifestV3 {
  const nameType = /^dev/.test(mode) ? '测试' : '生产'
  let actionType = ''

  if(nameType === '生产') {
    actionType = /^prod(.+)H$/.test(mode) ? '_手动' : '_自动'
  }

  let description = pkg.description
  let name = pkg.displayName ?? pkg.name

  // MALT: medium and long term
  if(mode.includes('MALT')) name = '中长期_' + name

  description = `${nameType}_${description}`
  name = `${nameType}_${modeAccounts[mode]}${actionType}_${name}`

  return {
    author: pkg.author,
    description,
    name,
    version: pkg.version,
    manifest_version: 3,
    ...replaceIcons(mode),
  };
}
